/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.movil;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.ejb.EJB;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import negocio.GestorFotosLocal;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Ing. Monica Merlo, Ing. Agustin  Fernandez
 */
public class VerFotoAppMovilServlet extends HttpServlet {

    static Logger logger = Logger.getLogger(SubirFotoAppMovilServlet.class);

    public static void log4jArchivo(Exception e) {
        /**
         * Primero obtenemos la url del archivo de configuración,
         * como están en el mismo paquete que la clase Main lo podemos obtener con el método getResource().
         */
        URL url = SubirFotoAppMovilServlet.class.getResource("/configuracion/log4j.properties");
        /**
         * Despues cargamos el archivo de configuración
         */
        PropertyConfigurator.configure(url);
        logger.error(e);
    }
    @EJB(name = "GestorFotosBean")
    private GestorFotosLocal gestorFotosBean;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String urlFoto = request.getParameter("urlFoto");
            String anchoFoto = request.getParameter("anchoFoto");
            String altoFoto = request.getParameter("altoFoto");
            String pathReal = (String) getServletContext().getRealPath(urlFoto);
            File fUrlFoto = new File(pathReal);
            BufferedImage bi = gestorFotosBean.verFotoDesdeCelular(fUrlFoto, Integer.parseInt(anchoFoto), Integer.parseInt(altoFoto));

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", stream);
            byte[] fotoEscalada = stream.toByteArray();
            out.write(Base64.encode(fotoEscalada));
        } catch (Exception e) {
            LoginAppMovilServlet.log4jArchivo(e);
            out.write("Error");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
